package com.acwing.partition12;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/1/18 12:17
 */
public class AC1141局域网 {

    private static final int N = 110, M = 210, INF = 0x3f3f3f3f;
    private static int[][] edges = new int[M][3];
    private static int n = 0, m = 0, cnt = 0;
    private static UnionFind uf = new UnionFind(N);

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] s = reader.readLine().split("\\s+");
        n = Integer.parseInt(s[0]);
        m = Integer.parseInt(s[1]);
        for (int i = 0; i < m; i++) {
            s = reader.readLine().split("\\s+");
            int u = Integer.parseInt(s[0]), v = Integer.parseInt(s[1]), w = Integer.parseInt(s[2]);
            edges[cnt++] = new int[]{u, v, w};
        }
        writer.write(kruskal() + "\n");
        writer.flush();
    }

    private static int kruskal() {
        int answer = 0;
        Arrays.sort(edges, 0, m, (o1, o2) -> Integer.compare(o1[2], o2[2]));
        for (int i = 0; i < m; i++) {
            int u = edges[i][0], v = edges[i][1], w = edges[i][2];
            if (uf.find(u) != uf.find(v)) uf.union(u, v);
            else answer += w;
        }
        return answer;
    }

    private static class UnionFind {
        private int[] parent;

        public UnionFind(int n) {
            parent = new int[n];
            for (int i = 0; i < n; i++) parent[i] = i;
        }

        public void union(int x, int y) {
            int a = find(x), b = find(y);
            if (a != b) parent[b] = a;
        }

        public int find(int x) {
            if (parent[x] == x) return x;
            return parent[x] = find(parent[x]);
        }
    }
}
